{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.4 \u8bfb\u5199\u5b57\u8282\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6bd4\u5982\u56fe\u7247\uff0c\u58f0\u97f3\u6587\u4ef6\u7b49\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u6a21\u5f0f\u4e3a rb \u6216 wb \u7684 open() \u51fd\u6570\u6765\u8bfb\u53d6\u6216\u5199\u5165\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Read the entire file as a single byte string\nwith open('somefile.bin', 'rb') as f:\n    data = f.read()\n\n# Write binary data to a file\nwith open('somefile.bin', 'wb') as f:\n    f.write(b'Hello World')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\u9700\u8981\u6307\u660e\u7684\u662f\u6240\u6709\u8fd4\u56de\u7684\u6570\u636e\u90fd\u662f\u5b57\u8282\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\uff0c\u800c\u4e0d\u662f\u6587\u672c\u5b57\u7b26\u4e32\u3002\n\u7c7b\u4f3c\u7684\uff0c\u5728\u5199\u5165\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u4fdd\u8bc1\u53c2\u6570\u662f\u4ee5\u5b57\u8282\u5f62\u5f0f\u5bf9\u5916\u66b4\u9732\u6570\u636e\u7684\u5bf9\u8c61(\u6bd4\u5982\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u5b57\u8282\u6570\u7ec4\u5bf9\u8c61\u7b49)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u65f6\u5019\uff0c\u5b57\u8282\u5b57\u7b26\u4e32\u548c\u6587\u672c\u5b57\u7b26\u4e32\u7684\u8bed\u4e49\u5dee\u5f02\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u6f5c\u5728\u7684\u9677\u9631\u3002\n\u7279\u522b\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7d22\u5f15\u548c\u8fed\u4ee3\u52a8\u4f5c\u8fd4\u56de\u7684\u662f\u5b57\u8282\u7684\u503c\u800c\u4e0d\u662f\u5b57\u8282\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Text string\nt = 'Hello World'\nt[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for c in t:\n    print(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Byte string\nb = b'Hello World'\nb[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for c in b:\n    print(c)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4ece\u4e8c\u8fdb\u5236\u6a21\u5f0f\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6216\u5199\u5165\u6587\u672c\u6570\u636e\uff0c\u5fc5\u987b\u786e\u4fdd\u8981\u8fdb\u884c\u89e3\u7801\u548c\u7f16\u7801\u64cd\u4f5c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('somefile.bin', 'rb') as f:\n    data = f.read(16)\n    text = data.decode('utf-8')\n\nwith open('somefile.bin', 'wb') as f:\n    text = 'Hello World'\n    f.write(text.encode('utf-8'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e8c\u8fdb\u5236I/O\u8fd8\u6709\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u7684\u7279\u6027\u5c31\u662f\u6570\u7ec4\u548cC\u7ed3\u6784\u4f53\u7c7b\u578b\u80fd\u76f4\u63a5\u88ab\u5199\u5165\uff0c\u800c\u4e0d\u9700\u8981\u4e2d\u95f4\u8f6c\u6362\u4e3a\u81ea\u5df1\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\nnums = array.array('i', [1, 2, 3, 4])\nwith open('data.bin','wb') as f:\n    f.write(nums)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u9002\u7528\u4e8e\u4efb\u4f55\u5b9e\u73b0\u4e86\u88ab\u79f0\u4e4b\u4e3a\u201d\u7f13\u51b2\u63a5\u53e3\u201d\u7684\u5bf9\u8c61\uff0c\u8fd9\u79cd\u5bf9\u8c61\u4f1a\u76f4\u63a5\u66b4\u9732\u5176\u5e95\u5c42\u7684\u5185\u5b58\u7f13\u51b2\u533a\u7ed9\u80fd\u5904\u7406\u5b83\u7684\u64cd\u4f5c\u3002\n\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5199\u5165\u5c31\u662f\u8fd9\u7c7b\u64cd\u4f5c\u4e4b\u4e00\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u5bf9\u8c61\u8fd8\u5141\u8bb8\u901a\u8fc7\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 readinto() \u65b9\u6cd5\u76f4\u63a5\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u5176\u5e95\u5c42\u7684\u5185\u5b58\u4e2d\u53bb\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\na = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0])\nwith open('data.bin', 'rb') as f:\n    f.readinto(a)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\u4f7f\u7528\u8fd9\u79cd\u6280\u672f\u7684\u65f6\u5019\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u5177\u6709\u5e73\u53f0\u76f8\u5173\u6027\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u4f9d\u8d56\u5b57\u957f\u548c\u5b57\u8282\u987a\u5e8f(\u9ad8\u4f4d\u4f18\u5148\u548c\u4f4e\u4f4d\u4f18\u5148)\u3002\n\u53ef\u4ee5\u67e5\u770b5.9\u5c0f\u8282\u4e2d\u53e6\u5916\u4e00\u4e2a\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u53ef\u4fee\u6539\u7f13\u51b2\u533a\u7684\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}